//
//  MineResaveViewController.m
//  NewProject
//
//  Created by 李赐岩 姜雪 张昕锐 on 15/3/10.
//  Copyright (c) 2015年 李赐岩 姜雪 张昕锐. All rights reserved.
//

#import "MineResaveViewController.h"

@interface MineResaveViewController ()

@end

@implementation MineResaveViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.segment.selectedSegmentIndex = 0;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"四季菜谱收藏", @"分类菜谱收藏", nil]];
    
    self.segment.frame = CGRectMake(self.view.frame.size.width / 5, 15, self.view.frame.size.width * 3 / 5, 30);
    self.navigationItem.titleView = self.segment;
    [self.segment addTarget:self action:@selector(controlPressed:) forControlEvents:UIControlEventValueChanged];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0 / 255.0 green:147/255.0 blue:52/ 255.0 alpha:0.9];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    //创建tableView
    self.tableV = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:(UITableViewStylePlain)];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    
    
    // Do any additional setup after loading the view.
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
//反转动画效果
-(void)controlPressed:(id)sender
{
    NSInteger segIndex = [self.segment selectedSegmentIndex];
    if (segIndex == 1) {
        
        MyClassResaveViewController *myClaVC = [[MyClassResaveViewController alloc] init];
        CATransition *animation = [CATransition animation];
        [animation setDuration:1.0];
        animation.type = @"oglFlip";
        [animation setSubtype:kCATransitionFromRight];
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
        
        [self.navigationController.view.layer addAnimation:animation forKey:nil];
        [self.navigationController pushViewController:myClaVC animated:NO];
        
    }
    
}

//必须实现的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [[CollectionDataBase shareCollectionDataBase] openDB];
    
     return [[CollectionDataBase shareCollectionDataBase] selectSpring].count;
  
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndex = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndex];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellIndex];
    }
    //cell上显示对象的名字
    
    [[CollectionDataBase shareCollectionDataBase] createTable];
    self.array = [[CollectionDataBase shareCollectionDataBase] selectSpring];
    self.springModel = [self.array objectAtIndex:indexPath.row];
    cell.textLabel.text = self.springModel.name;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SpringDetailViewController *detail = [[SpringDetailViewController alloc] init];
    SpringModel *model = [self.array objectAtIndex:indexPath.row];
    detail.id = model.id;
    detail.methodLable.text = model.message;
    [self.navigationController pushViewController:detail animated:YES];
    
    
}





//删除类型
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableV setEditing:editing animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //先删库
    [[CollectionDataBase shareCollectionDataBase] openDB];
    SpringModel *data = [[[CollectionDataBase shareCollectionDataBase] selectSpring] objectAtIndex:indexPath.row];
    [[CollectionDataBase shareCollectionDataBase] deleteSpring:data.number];
    //再删cell内容
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:(UITableViewRowAnimationLeft)];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
